package org.example.dao.impl;

import org.example.dao.MaterialDao;
import org.example.entity.Material;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MaterialDaoImpl implements MaterialDao {

    @Override
    public int insert(Connection conn, Material material) throws SQLException {
        String sql = "insert into MATERIAL values(NULL,?,?,?,?)";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setObject(1, material.getMaterialName());
        statement.setObject(2, material.getMaterialImage());
        statement.setObject(3, material.getStock());
        statement.setObject(4, material.getWarningStock());
        return statement.executeUpdate();
    }

    @Override
    public boolean exists(Connection conn, Material material) throws SQLException {
        String sql = "select * from MATERIAL where MATERIAL_NAME = ?";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, material.getMaterialName());
        ResultSet resultSet = statement.executeQuery();
        return resultSet.next();
    }
}
